package com.mioto.ld.module.device.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.module.device.model.RecoveryFaultBO;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.device.dao.DeviceFaultLogDao;
import com.mioto.ld.module.device.model.DeviceFaultLog;
import com.mioto.ld.module.device.service.IDeviceFaultLogService;
import javax.annotation.Resource;
import java.util.*;

/**
 *
 * @author mioto
 * @date 2024-05-30 20:17:34
 */
@Service("deviceFaultLogService")
public class DeviceFaultLogServiceImpl implements IDeviceFaultLogService{

    @Resource
    private DeviceFaultLogDao deviceFaultLogDao;

    @Override
    public List<DeviceFaultLog> findList(DeviceFaultLog deviceFaultLog) {
        return deviceFaultLogDao.findList(deviceFaultLog);
    }

    @Override
    public int insert(DeviceFaultLog deviceFaultLog) {
        return deviceFaultLogDao.insertIgnoreNull(deviceFaultLog);
    }

    @Override
    public int update(DeviceFaultLog deviceFaultLog) {
        return deviceFaultLogDao.update(deviceFaultLog);
    }

    @Override
    public int updateIgnoreNull(DeviceFaultLog deviceFaultLog) {
        return deviceFaultLogDao.updateIgnoreNull(deviceFaultLog);
    }

    @Override
    public DeviceFaultLog findByColumn(String column, Object value) {
        return deviceFaultLogDao.findByColumn(column,value);
    }

    @Override
    public int deleteByColumn(String column, Object value) {
        return deviceFaultLogDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return deviceFaultLogDao.batchDelete(ids);
    }

    @Override
    public int updateRecoveryDate(Integer deviceId, Integer[] faultTypes, Date recoveryDate) {
        return deviceFaultLogDao.updateRecoveryDate(deviceId,faultTypes,recoveryDate);
    }

    @Override
    public int existFault(int deviceId, int faultType) {
        return deviceFaultLogDao.existFault(deviceId,faultType);
    }

    @Override
    public Map<Integer, String> findRecoveryFault(Integer deviceId, Integer... faultTypes) {
        List<RecoveryFaultBO> list = deviceFaultLogDao.findRecoveryFault(deviceId,faultTypes);
        Map<Integer, String> tempMap = new HashMap<>(2);
        if (CollUtil.isNotEmpty(list)){
            RecoveryFaultBO recoveryFault;
            for (Integer faultType : faultTypes) {
                recoveryFault = list.stream().filter(recoveryFaultBO -> faultType.equals(recoveryFaultBO.getFaultType()))
                        .max(Comparator.comparing(RecoveryFaultBO::getReportDate)).orElse(null);
                if (ObjectUtil.isNotNull(recoveryFault) && recoveryFault.getFaultStatus() == 1){
                    tempMap.put(recoveryFault.getFaultType(),recoveryFault.getEventName());
                }
            }
        }
        return tempMap;
    }

    @Override
    public int[] findDeviceFault(Date startYearDate,Date startMonthDate,Date endDate) {
        return deviceFaultLogDao.findDeviceFault(startYearDate,startMonthDate,endDate);
    }
}